/* global window */
import { message } from 'antd'
import api from 'api'
import modelExtend from 'dva-model-extend'
import { pathMatchRegexp } from 'utils'
import { pageModel } from 'utils/model'
import moment from 'moment'

const {
  queryMissions,
  deleteMission,
  PauseMission,
  StopMission,
  ResumeMission,
  reStartMission,
  subscribeMissionNotify,
  unsubscribeMissionNotify,
} = api

const regionstore = {}

export default modelExtend(pageModel, {
  namespace: 'cmdlist',

  state: {
    ...regionstore,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (pathMatchRegexp(`/cmdlist`, location.pathname)) {
          let payload = { page: 1, pageSize: 10 }
          if (location.query) {
            const { page, pageSize } = location.query
            payload = {
              ...payload,
              ...(page != undefined && pageSize != undefined
                ? { page: Number(page), pageSize: Number(pageSize) }
                : {}),
              // ...(project != undefined ? { project } : {}),
              // ...(filter ? { filter: JSON.parse(filter) } : {}),
            }
          }
          dispatch({
            type: 'queryMissions',
            payload,
          })
        }
      })
    },
  },

  effects: {
    *clearstore({ payload = {} }, { call, put, select }) {},
    *queryMissions({ payload }, { call, put, select }) {
      const { result } = yield call(queryMissions, payload)
      if (result && result.data != undefined) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: result.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: result.total,
            },
          },
        })
      }
    },
    *onGetResponse({ payload }, { call, put, select }) {
      if (payload) {
        const { list } = yield select(_ => _.cmdlist)
        for (var index = 0; index < list.length; index++) {
          if (list[index]) {
            if (list[index].missionId == payload.missionId) {
              list[index]['Responsed'] = payload.Responsed
              list[index]['Status'] = payload.Status
              list[index]['CostTime'] = payload.CostTime
            }
          }
        }
        yield put({
          type: 'updateState',
          payload: {
            list: JSON.parse(JSON.stringify(list)),
          },
        })
      }
    },
    *deleteMission({ payload }, { call, put, select }) {
      message.loading('删除中...')
      const { result } = yield call(deleteMission, payload)
      message.destroy()
      message.success('删除成功！')
    },
    *PauseMission({ payload }, { call, put, select }) {
      message.loading('暂停中...')
      const { result } = yield call(PauseMission, payload)
      message.destroy()
      message.success('暂停成功！')
    },
    *StopMission({ payload }, { call, put, select }) {
      message.loading('停止中...')
      const { result } = yield call(StopMission, payload)
      message.destroy()
      message.success('停止成功！')
    },
    *ResumeMission({ payload }, { call, put, select }) {
      message.loading('恢复中...')
      const { result } = yield call(ResumeMission, payload)
      message.destroy()
      message.success('恢复成功！')
    },
    *reStartMission({ payload }, { call, put, select }) {
      message.loading('重启中...')
      const { result } = yield call(reStartMission, payload)
      message.destroy()
      message.success('重启成功！')
    },
    *getMissionDetail({ payload }, { call, put, select }) {
      const { result } = yield call(getMissionDetail, payload)
    },
    *subscribeMissionNotify({ payload }, { call, put, select }) {
      const { result } = yield call(subscribeMissionNotify, payload)
    },
    *unsubscribeMissionNotify({ payload }, { call, put, select }) {
      const { result } = yield call(unsubscribeMissionNotify, payload)
    },
  },

  reducers: {},
})
